package com.txm.drawPic;
/*
* 所有坦克的基类
*
* */
import java.awt.*;
import java.util.Arrays;
import java.util.Vector;

@SuppressWarnings({"all"})
public class Tank {
    public static final int PANEL_W = 800;
    public static final int PANEL_H = 500;

    private int multiple = 1;

    private int speed = 2;

    private int track_width = 10;
    private int track_height = 60;
    private int main_width = 30;
    private int track_main_difference = 10;

    private Color color = null;
    private Color color_alpha = null;

    public static MyPanel MYPANEL = null;
    private int x = 0;
    private int y = 0;
    private String mark = "";
    private String direct = "";

    private Vector<Bullet> bullet = new Vector<>();
    private int[] bulletStratPosition = new int[2];

    private boolean isLive = true;

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        if (live == false) {
            MyPanel.Bombs.add(new Bomb(x, y));
//            MYPANEL.repaint();
        }
        isLive = live;
    }

    public int getMultiple() {
        return multiple;
    }

    public void setMultiple(int multiple) {
        this.multiple = multiple;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getTrack_width() {
        return track_width;
    }

    public void setTrack_width(int track_width) {
        this.track_width = track_width;
    }

    public int getTrack_height() {
        return track_height;
    }

    public void setTrack_height(int track_height) {
        this.track_height = track_height;
    }

    public int getMain_width() {
        return main_width;
    }

    public void setMain_width(int main_width) {
        this.main_width = main_width;
    }

    public int getTrack_main_difference() {
        return track_main_difference;
    }

    public void setTrack_main_difference(int track_main_difference) {
        this.track_main_difference = track_main_difference;
    }

    public Color getColor() {
        return color;
    }


    public void setColor(Color color) {
        this.color = color;
    }

    public Color getColor_alpha() {
        return color_alpha;
    }

    public void setColor_alpha(Color color_alpha) {
        this.color_alpha = color_alpha;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    public String getDirect() {
        return direct;
    }

    public void setDirect(String direct) {
        this.direct = direct;
    }

    public int[] getBulletStratPosition() {
        int[] arr = new int[2];
        if (direct.equals("1")) {
            arr[0] = bulletStratPosition[0];
            arr[1] = bulletStratPosition[1] - 1;
        } else if (direct.equals("2")) {
            arr[0] = bulletStratPosition[0] + 1;
            arr[1] = bulletStratPosition[1];
        } else if (direct.equals("3")) {
            arr[0] = bulletStratPosition[0];
            arr[1] = bulletStratPosition[1] + 1;
        } else if (direct.equals("4")) {
            arr[0] = bulletStratPosition[0] - 1;
            arr[1] = bulletStratPosition[1];
        }
        return arr;
    }


    public void setBulletStratPosition(int x, int y) {
//        System.out.println(x + " " + y)
        this.bulletStratPosition[0] = x;
        this.bulletStratPosition[1] = y;
    }

    public Vector<Bullet> getBullet() {
        return bullet;
    }

    public void removeBullet(Bullet bullet) {
        this.bullet.remove(bullet);
    }

    @Override
    public String toString() {
        return "\nTank{" +
                "multiple=" + multiple +
                ", speed=" + speed +
                ", track_width=" + track_width +
                ", track_height=" + track_height +
                ", main_width=" + main_width +
                ", track_main_difference=" + track_main_difference +
                ", color=" + color +
                ", color_alpha=" + color_alpha +
                ", x=" + x +
                ", y=" + y +
                ", mark='" + mark + '\'' +
                ", direct='" + direct + '\'' +
                ", bulletStratPosition=" + Arrays.toString(bulletStratPosition) +
                '}';
    }

    public boolean isCou(int x, int y) {
        boolean bool = false;
        for (Tank ob : MyPanel.obstacle) {
            if (ob == null) bool = false;
            if (ob != this && this.isIntersect(x, y, ob)) {
                System.out.println(getMark() + "与" + ob.getMark() + "发生碰撞。");
                bool = true;
                break;
            } else bool = false;
        }
        return bool;
    }

    public void moveUp() {
        setDirect("1");
        if (y - speed < 0) {
            return;
        }
        int new_val = y - speed;
        if (isCou(x, new_val)) return;
        setY(new_val);
    }

    public void moveDown() {
        setDirect("3");
        if (y + speed + track_height > PANEL_H) {
            return;
        }
        int new_val = y + speed;
        if (isCou(x, new_val)) return;
        setY(new_val);
    }

    public void moveRight() {
        setDirect("2");
        if (x + speed + track_height > PANEL_W) {
            return;
        }
        int new_val = x + speed;
        if (isCou(new_val, y)) return;
        setX(new_val);
    }

    public void moveLeft() {
        setDirect("4");
        if (x - speed < 0) {
            return;
        }
        int new_val = x - speed;
        if (isCou(new_val, y)) return;
        setX(new_val);
    }

    //  发射子弹
    public void shut() {
        Bullet bullet = new Bullet(getBulletStratPosition()[0], getBulletStratPosition()[1], direct, this);
        this.bullet.add(bullet);
        bullet.start();
    }

    public int[] getArea(String direct) {
        int[] ints = null;
        switch (direct) {
            case "1":
            case "3":
                ints = new int[]{track_width * 2 + main_width, track_height};
                break;
            case "2":
            case "4":
                ints = new int[]{track_height, track_width * 2 + main_width};
                break;
        }
        return ints;
    }

    //  判断是否和坦克发生碰撞
    public boolean isIntersect(int cur_x, int cur_y, Tank tank) {
        boolean bool = false;
        String tank_D = tank.getDirect();
        switch (direct) {
            case "1": {
                int x = cur_x;
                int y = cur_y;
                int x1 = cur_x + track_width * 2 + main_width;
                int y1 = cur_y;
                if (tank_D.equals("1") || tank_D.equals("3")) {
                    int tank_x = tank.getX();
                    int tank_y = tank.getY() + track_height;
                    int tank_x1 = tank.getX() + track_width * 2 + main_width;
                    int tank_y1 = tank.getY() + track_height;
                    if (((x >= tank_x && x <= tank_x1) || (x1 >= tank_x && x1 <= tank_x1)) && y1 == tank_y) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                } else {
                    int tank_x = tank.getX();
                    int tank_y = tank.getY() + track_width * 2 + main_width;
                    int tank_x1 = tank.getX();
                    int tank_y1 = tank.getY() + track_width * 2 + main_width;
                    if (((x >= tank_x && x <= tank_x1) || (x1 >= tank_x && x1 <= tank_x1)) && y1 == tank_y) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                }
            }
            break;
            case "2": {
                int x = cur_x + track_height;
                int y = cur_y;
                int x1 = cur_x + track_height;
                int y1 = cur_y + track_width * 2 + main_width;
                if (tank_D.equals("1") || tank_D.equals("3")) {
                    int tank_x = tank.getX();
                    int tank_y = tank.getY();
                    int tank_x1 = tank.getX();
                    int tank_y1 = tank.getY() + track_height;
                    if (((y >= tank_y && y <= tank_y1) || (y1 >= tank_y && y1 <= tank_y1)) && x == tank_x) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                } else {
                    int tank_x = tank.getX();
                    int tank_y = tank.getY();
                    int tank_x1 = tank.getX();
                    int tank_y1 = tank.getY() + track_width * 2 + main_width;
                    if (((y >= tank_y && y <= tank_y1) || (y1 >= tank_y && y1 <= tank_y1)) && x == tank_x) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                }
            }

            break;
            case "3": {
                int x = cur_x;
                int y = cur_y + track_height;
                int x1 = cur_x + track_width * 2 + main_width;
                int y1 = cur_y + track_height;
                if (tank_D.equals("1") || tank_D.equals("3")) {
                    int tank_x = tank.getX();
                    int tank_y = tank.getY();
                    int tank_x1 = tank.getX() + track_width * 2 + main_width;
                    int tank_y1 = tank.getY();
                    if (((x >= tank_x && x <= tank_x1) || (x1 >= tank_x && x1 <= tank_x1)) && y1 == tank_y) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                } else {
                    int tank_x = tank.getX();
                    int tank_y = tank.getY();
                    int tank_x1 = tank.getX() + track_height;
                    int tank_y1 = tank.getY();
                    if (((x >= tank_x && x <= tank_x1) || (x1 >= tank_x && x1 <= tank_x1)) && y1 == tank_y) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                }
            }
            break;
            case "4": {
                int x = cur_x;
                int y = cur_y;
                int x1 = cur_x;
                int y1 = cur_y + track_width * 2 + main_width;
                if (tank_D.equals("1") || tank_D.equals("3")) {
                    int tank_x = tank.getX() + track_width * 2 + main_width;
                    int tank_y = tank.getY();
                    int tank_x1 = tank.getX() + track_width * 2 + main_width;
                    int tank_y1 = tank.getY() + track_height;
                    if (((y >= tank_y && y <= tank_y1) || (y1 >= tank_y && y1 <= tank_y1)) && x == tank_x) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                } else {
                    int tank_x = tank.getX() + track_height;
                    int tank_y = tank.getY();
                    int tank_x1 = tank.getX() + track_height;
                    int tank_y1 = tank.getY() + track_width * 2 + main_width;
                    if (((y >= tank_y && y <= tank_y1) || (y1 >= tank_y && y1 <= tank_y1)) && x == tank_x) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                }
            }
            break;
        }
        return bool;
    }


}
